﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;

namespace project_assess.assess_relate
{
    public partial class work_frm : Office2007Form
    {
        Mfrm mfrm;
        DataBase m_db;
        public work_frm(Mfrm frm)
        {
            InitializeComponent();
            mfrm = frm;
            m_db = frm.m_db;
            load_tree();
        }
        #region 选择工程
        private void btn_chooseprj_Click(object sender, EventArgs e)
        {
            choose_prj frm = new choose_prj(mfrm);
            frm.ShowDialog(this);
            load_tree();
        }
        #endregion
        #region 加载工程考核工作量
        void load_tree()
        {
            if (mfrm.curprjid == null || mfrm.curprjid == "")
            {
                return;
            }

            
            tree.Nodes.Clear();
            Node nn = new Node();
            nn.Text = mfrm.curprjid;
            string sql;
            DataTable dt;
            sql = "select * from [assess_work] where [项目编号]='" + mfrm.curprjid + "' order by [终止时间]";
            dt = m_db.GetDataSet(sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Node n = new Node();
                n.Text = string.Format("第{0}次", (i + 1));
                n.Cells.Add(new Cell(dt.Rows[i]["起始比例"].ToString() + "%"));
                n.Cells.Add(new Cell(Convert.ToDateTime(dt.Rows[i]["起始时间"]).ToShortDateString()));
                n.Cells.Add(new Cell(dt.Rows[i]["终止比例"].ToString() + "%"));
                n.Cells.Add(new Cell(Convert.ToDateTime(dt.Rows[i]["终止时间"]).ToShortDateString()));
                n.Cells.Add(new Cell(dt.Rows[i]["项目部提取额"].ToString()));
                n.Cells.Add(new Cell(dt.Rows[i]["班组提取额"].ToString()));
                nn.Nodes.Add(n);
                if (i == dt.Rows.Count - 1)
                {
                    dateTimeInput1.Value = Convert.ToDateTime(dt.Rows[i]["终止时间"]);
                    dateTimeInput1.Enabled = false;
                    slider1.Value = (int)(Convert.ToDouble(dt.Rows[i]["终止比例"]) );
                    slider1.Enabled = false;
                    slider2.Value = slider1.Value;
                }
            }
            if (dt.Rows.Count == 0)
            {
                dateTimeInput1.Enabled = true;
                slider1.Enabled = true;
                dateTimeInput1.Text = "";
                slider1.Value = 0;
                dateTimeInput2.Text = "";
                slider2.Value = 0;

            }
            nn.Expanded = true;
            tree.Nodes.Add(nn);
        }
        #endregion
        #region 数值显示
        private void slider1_ValueChanged(object sender, EventArgs e)
        {
            l1.Text = slider1.Value.ToString() + "%";
        }

        private void slider2_ValueChanged(object sender, EventArgs e)
        {
            l2.Text = slider2.Value.ToString() + "%";
        }
        #endregion

        private void btnassess_Click(object sender, EventArgs e)
        {
            message_frm msg;
            if (mfrm.curprjid == null || mfrm.curprjid == "")
            {
                msg = new message_frm("没有选择项目,请先选择项目!", "错误", icon_img.warning);
                msg.ShowDialog(this);
                msg.Close();
                return;
            }
            string sql = "select [工作量] from [projects] where [项目编号]='"
                + mfrm.curprjid + "'";
            DataTable dt = m_db.GetDataSet(sql).Tables[0];
            double max = Convert.ToDouble(dt.Rows[0][0]);
            double ratio1, ratio2;
            DateTime time1, time2;
            time1 = dateTimeInput1.Value;
            time2 = dateTimeInput2.Value;
            ratio1 = (double)slider1.Value / 100; ratio2 = (double)slider2.Value / 100;
            TimeSpan sp = time2 - time1;
            double ratio = ratio2 - ratio1;
            
            if (sp.Days <= 0)
            {
                msg = new message_frm("时间填写错误,应为递增", "错误", icon_img.warning);
                msg.ShowDialog(this);
                msg.Close();
                return;
            }
            if (ratio < 0)
            {
                msg = new message_frm("进度填写错误,应为递增", "错误", icon_img.warning);
                msg.ShowDialog(this);
                msg.Close();
                return;
            }
            if (ratio1 == 1)
            {
                msg = new message_frm("已完成考核,无法再继续考核", "错误", icon_img.warning);
                msg.ShowDialog(this);
                msg.Close();
                return;
            }
            msg = new message_frm("是否添加考核内容", "提示", icon_img.query);
            msg.ShowDialog(this);
            if (msg.yesorno == false)
            {
                msg.Close();
                return;
            }
            msg.Close();
            double prjfee = ratio * max * mfrm.get_para("工作量项目额度") * mfrm.get_para("工作量提取额度");
            double teamfee = ratio * max * mfrm.get_para("工作量班组额度") * mfrm.get_para("工作量提取额度");
            prjfee = Math.Round(prjfee, 2);
            teamfee = Math.Round(teamfee, 2);
            double no = 1;

            sql = "Insert into [assess_work] values('" + mfrm.curprjid + "'," + no
                + ",'" + time1.ToShortDateString() + "'," + ratio1 * 100 + ",'" + time2.ToShortDateString()
            + "'," + ratio2 * 100 + "," + prjfee + "," + teamfee + ",'','','','')";
            m_db.ExecuteSQL(sql);
            load_tree();
        }


        //提供删除
        private void tree_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                Node xn = tree.SelectedNode;
                if (xn != null && xn.Level > 0)
                {
                    message_frm msg = new message_frm("是否删除该项", "询问", icon_img.query);
                    msg.ShowDialog(this);
                    if (msg.yesorno == false)
                    {
                        msg.Close();
                        return;
                    }
                    string sql = string.Format(
                        "Delete from [assess_work] where 项目编号='{0}' "+
                    "and 项目部提取额={1} and 班组提取额={2}",
                        mfrm.curprjid, xn.Cells[5].Text, xn.Cells[6].Text);
                    m_db.ExecuteSQL(sql);
                    load_tree();
                }
            }
        }
    }
}
